﻿using iot.models;
using iot.repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace iot.Forms
{
    public partial class AddDevcieFrm : Form
    {
        public AddDevcieFrm()
        {
            InitializeComponent();
        }


        private string filePath = string.Empty;

        private void saveDeviceBtn_Click(object sender, EventArgs e)
        {
            using (var db = new IotDbContext())
            {
                var deviceName = deviceNameInput.Text.ToString();
                var deviceDescription = deviceDescInput.Text.ToString();
                var deviceStatus = deviceStatusSelect.SelectedValue;
                var deviceType = deviceTypeSelect.SelectedValue;
                db.devices?.Add(new Device
                {
                    Id = Guid.NewGuid().ToString(),
                    DeviceCode = deviceName,
                    DeviceName = deviceName,
                    Description = deviceDescription,
                    Status = deviceStatus?.ToString(),
                    Img = "https://img.icons8.com/ios-filled/50/000000/device.png",
                    DeviceType = deviceType?.ToString(),
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                });
                db.SaveChanges();
                MessageBox.Show("添加成功");
                deviceNameInput.Text = "";
                deviceDescInput.Text = "";
                deviceStatusSelect.SelectedIndex = -1;
                deviceTypeSelect.SelectedIndex = -1;
                deviceNameInput.Focus();
                deviceDescInput.Focus();
            }
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openFIleDialogBtn_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName; // Corrected variable assignment
                fileName.Text = openFileDialog.FileName;
            }
        }

        private async void uploadFileBtn_Click(object sender, EventArgs e)
        {
            string address = "http://localhost:8080";
            string[] files = { filePath };
            using (HttpClient client = new HttpClient())
            {
                using (var content = new MultipartFormDataContent())
                {
                    foreach (string fileFullName in files)
                    {
                        if (File.Exists(fileFullName))
                        {
                            var fileInfo = new FileInfo(fileFullName);
                            var fileStream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read);
                            var streamContent = new StreamContent(fileStream);
                            content.Add(streamContent, "file", fileInfo.Name);
                        }
                    }

                    try
                    {
                        HttpResponseMessage response = await client.PostAsync(address, content);
                        string responseContent = await response.Content.ReadAsStringAsync();
                        string statusCode = response.StatusCode.ToString();
                        MessageBox.Show($"上传成功: {statusCode}");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"上传失败: {ex.Message}");
                    }
                }
            }
        }
    }
}
